﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace WindowsFormsApplication1.wtyczki
{
    public static class Morfologic
    {
        public static Bitmap erozja(Bitmap bm, int color)
        {
            Bitmap bmTmp = new Bitmap(bm);
            int pix=0;
            for (int y = 1; y < bmTmp.Height-1; y++)
            {
                for (int x = 1; x < bmTmp.Width-1; x++)
                {
                    if (chckPX(x, y, bm))
                    {
                        bmTmp.SetPixel(x, y, bm.GetPixel(x, y));
                    }
                    else
                    {
                        bmTmp.SetPixel(x, y, Color.White);//Color.FromArgb(bm.GetPixel(x,y).R/2,bm.GetPixel(x,y).G/2,bm.GetPixel(x,y).B/2));
                        pix++;
                    }
                    
                }
            }
            
            return bmTmp;
        }
        private static bool chckPX(int x, int y, Bitmap bm)
        {

            bool flag = true;
            for (int i = -1; i < 1; i++)
            {
                for (int j = -1; j < 1; j++)
                {
                    if (Math.Abs( bm.GetPixel(x, y).R-bm.GetPixel(x+i,y+j).R)>10 ||
                        Math.Abs( bm.GetPixel(x, y).G-bm.GetPixel(x+i,y+j).G)>10 ||
                        Math.Abs( bm.GetPixel(x, y).B-bm.GetPixel(x+i,y+j).B)>10 )
                    {
                        flag = false;
                    }
                }
            }
            return flag;
        }
    }

}
